package com.lee.boot.batch.step;

import com.lee.boot.batch.common.YesOrNoEnum;
import com.lee.boot.batch.model.Product;
import com.lee.boot.batch.model.ProductDO;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.mybatis.spring.batch.builder.MyBatisCursorItemReaderBuilder;
import org.springframework.batch.core.*;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.stereotype.Component;

@Component("fileProcessor")
public class FileProcessor implements ItemProcessor<Product, ProductDO>, ItemProcessListener<Product, ProductDO>, StepExecutionListener {

    private final String value = YesOrNoEnum.YES.getValue();

    String a = "";

    /**
     * 使用mybatis从数据库中读数据的item
     *
     * @param
     * @return
     */
//    public ItemReader getMybatisItemReader(SqlSessionFactory sqlSessionFactory) {
//        MyBatisCursorItemReader<Object> build =
//                new MyBatisCursorItemReaderBuilder<>()
//                        .sqlSessionFactory(sqlSessionFactory)
//                        // mapper的id
//                        .queryId("getEntityByUsername").build();
//        return build;
//    }
    @Override
    public void beforeProcess(Product item) {
        System.out.println("process beforeProcess");

    }

    @Override
    public void afterProcess(Product item, ProductDO result) {
        System.out.println("process afterProcess");
    }

    @Override
    public void onProcessError(Product item, Exception e) {
        System.out.println("process onProcessError");
    }


    @Override
    public ProductDO process(Product item) throws Exception {
        System.out.println("process ");
        System.out.println(a);
        return null;
    }

    /**
     * Initialize the state of the listener with the {@link StepExecution} from
     * the current scope.
     *
     * @param stepExecution instance of {@link StepExecution}.
     */
    @Override
    public void beforeStep(StepExecution stepExecution) {
        a = "asdadasdas";
    }

    /**
     * Give a listener a chance to modify the exit status from a step. The value
     * returned will be combined with the normal exit status using
     * {@link ExitStatus#and(ExitStatus)}.
     * <p>
     * Called after execution of step's processing logic (both successful or
     * failed). Throwing exception in this method has no effect, it will only be
     * logged.
     *
     * @param stepExecution {@link StepExecution} instance.
     * @return an {@link ExitStatus} to combine with the normal value. Return
     * {@code null} to leave the old value unchanged.
     */
    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        a = null;
        return ExitStatus.COMPLETED;
    }
}
